#include<stdio.h>
#include<malloc.h>

int *a,n,i,ext[50];

void append(int ele)                 
{
 n++;
 a=(int*)realloc(a,n*sizeof(int));
 a[n-1]=ele;
}

void extend(int n2)
{ 
 a=(int*)realloc(a,(n+n2)*sizeof(int));
 for(i=0;i<n2;i++)
 { 
  a[n + i]=ext[i];
 }
 n=n+n2;
} 

void insert(int pos,int ins)
{
 n++;
 a=(int*)realloc(a,n*sizeof(int));
 for(i=n-1;i>pos-2;i--)
 { 
 a[i]=a[i-1];
 }
 a[pos-1]=ins; 
 
} 

int count(int cnt)
{
 int ct=0;
 for(i=0;i<n;i++)
 {
  if(a[i]==cnt)
  {
   ct++;
  }
 }
 return ct;
}

int indez(ind)
{
 for(i=0;i<n;i++)
 {
  if(a[i]==ind)
  {
  return i+1;
  }
 }
}

int remov(int rem)
{
 int r;
 for(i=0;i<n;i++)
 {
  if(a[i]==rem)
  {
   r=i;
  }
 }
 for(i=r;i<n;i++)
 {
  a[i]=a[i+1];
 }
 n--;
 a=(int*)realloc(a,n*sizeof(int));
}

int pop(int indpop)
{
 int p;
 p=a[indpop];
 for(i=indpop;i<n;i++)
 {
  a[i]=a[i+1];
 }
 n--;
 a=(int*)realloc(a,n*sizeof(int));
 return p;
} 

void sort()
{
 int temp;
for(i=0;i<n;i++)
 { 
  for(i=0;i<n-1;i++)
  {
   if(a[i]>a[i+1])
   {temp=a[i]; 
    a[i]=a[i+1];
    a[i+1]=temp;
   } 
  }
 }
}

void reverse()
{
 int *ptr,j;
 ptr = (int*) malloc(n*sizeof(int));
 for(i=n-1,j=0;i>=0,j<n;i--,j++)
 {
  ptr[j]=a[i];   
 }  
 for(j=0;j<n;j++)
 {
  a[j]=ptr[j];
 }  
}

void show_data()
{printf("\nThe new list is: [");
 for (i=0; i<n-1; i++) 
 {
  printf("%d,",a[i]);
 }
 {printf("%d]\n",a[n-1]);}   
}

void main()
{  
 int ch,c=1;
 int ele,n2;
 int pos,ins;
 int cnt,ct; 
 int ind,pos2;
 int rem; 
 int indpop,pop2; 
 printf("Enter the size of the array");
 scanf("%d", &n);
 a = (int*) malloc(n*sizeof(int));
 
 for (i=0; i<n; i++) 
 {     
  printf("Enter number %d: ", i+1);
  scanf("%d", &a[i]);
 }

 while(c==1)
 { 
 printf("Enter your choice: \n1. Append\n2.Extend\n3.Insert\n4.remove\n5.pop\n6.Index \n7.Count \n8.Sort \n9.Reverse\n10.Exit\n");
 scanf("%d",&ch);
 switch(ch)
 { 
  case 1:printf("Enter the element to append\n");
         scanf("%d",&ele);
         append(ele);show_data();  
         break;

  case 2:printf("Enter the number of elements you want to extend the list with\n");
         scanf("%d",&n2);         
         for(i=0;i<n2;i++)
         { printf("Enter the elements\n");
           scanf("%d",&ext[i]);
         } 
         extend(n2);
         show_data(); 
         break;

  case 3:printf("Enter the position where you want to insert the element");
         scanf("%d",&pos);
         printf("Enter the element that you want to insert");
         scanf("%d",&ins);    
         insert(pos,ins);
         show_data();
         break;

  case 4:printf("Enter the element to remove");
         scanf("%d",&rem);
         remov(rem);
         show_data(); 
         break;

  case 5:printf("Give the index of the number that is to be popped(index is starting at zero)");
         scanf("%d",&indpop);          
         pop2=pop(indpop);
         printf("The popped element is %d",pop2);          
         break;

  case 6:printf("Give the element whose position is to be found");
         scanf("%d",&ind);
         pos2=indez(ind);
         printf("The position of the element in the list is%d\n",pos2);
         break;

  case 7:printf("Enter the element to be counted");
         scanf("%d",&cnt);           
         ct=count(cnt);
         printf("The number of occurrences of%d is %d\n",cnt,ct);
         break; 

 case 8:sort();        
        show_data();
        break;

 case 9:reverse();
        show_data(); 
        break;   

  case 10:
          break;
}
 printf("Do you want to re-enter in the while loop(1 for yes any other digit for no)\n");
 scanf("%d",&c);
}
}
